#include "friendlabel.h"
#include "ui_friendlabel.h"

FriendLabel::FriendLabel(QWidget *parent)
    : QFrame(parent)
    , ui(new Ui::FriendLabel)
{
    ui->setupUi(this);

    ui->close_lb->setState("normal","hover","pressed",
                           "selected_normal","selected_hover","selected_pressed");

    connect(ui->close_lb, &ClickedLabel::clicked, this, &FriendLabel::slot_close);
}

FriendLabel::~FriendLabel()
{
    delete ui;
}

// 设置标签
void FriendLabel::SetText(QString text)
{
    m_text = text;
    ui->tip_lb->setText(m_text);
    ui->tip_lb->adjustSize();

    // 获取QLabel控件的字体信息
    QFontMetrics fontMetrics(ui->tip_lb->font());
    // 获取文本的宽度
    auto text_width = 0;
    for (int i = 0; i < ui->tip_lb->text().length(); ++i) {
        text_width += fontMetrics.horizontalAdvance(ui->tip_lb->text().at(i));
    }
    auto text_height = fontMetrics.height(); // 获取文本的高度

    qDebug() << " ui->tip_lb.width() is " << ui->tip_lb->width();
    qDebug() << " ui->close_lb->width() is " << ui->close_lb->width();
    qDebug() << " textWidth is " << text_width;

    // 调整标签的位置
    this->setFixedWidth(ui->tip_lb->width() + ui->close_lb->width() + 5);
    this->setFixedHeight(text_height + 2);

    qDebug()<< "  this->setFixedHeight " << this->height();
    m_width = this->width();
    m_height = this->height();
}

int FriendLabel::Width()
{
    return m_width;
}

int FriendLabel::Height()
{
     return m_height;
}

QString FriendLabel::Text()
{
    return m_text;
}

void FriendLabel::slot_close()
{
    emit sig_close(m_text);
}
